import pygame
from contants import*
from models import*




class Game:
    def __init__(self) -> None:
        pygame.init()
        
        pygame.display.set_caption("贪吃蛇大战v1.0")

        self.screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))

        icon = pygame.image.load("res/icon.png")

        pygame.display.set_icon(icon)

        bg_image = pygame.image.load("res/bg.png")
        #缩放
        self.bg_image = pygame.transform.scale(bg_image,(SCREEN_WIDTH,SCREEN_HEIGHT))

    def start(self):
        
        is_game_over = False    
        clock = pygame.time.Clock()
        snake = Snake(3,5)
        food = Food(Food.gen_food_positison(snake))

        while True:
            #处理事件，获取用户输入的事件
            event_list = pygame.event.get()
            new_dir = None
            #解析处理所有的事件
            #event.type 保存了用户输入的事件类型 :鼠标。案件
            #event.key 保存了按下或抬起的按键
            for event in event_list:
                if event.type == pygame.QUIT:
                    print("点击了关闭")
                    #退出游戏
                    pygame.display.quit()
                    exit(0)
                elif event.type == pygame.KEYDOWN:
                    print("按键按下：",event.key)
                    if is_game_over :
                        if event.key == pygame.K_q:
                            self.quit_game()
                        elif event.key == pygame.K_SPACE:
                            snake = Snake(5,8)
                            food = Food(Food.gen_food_positison(snake))
                            is_game_over = False   
                                
                    #上。下。左。右。空格
                    

                    elif   event.key == pygame.K_ESCAPE:  
                        print("关闭")
                        self.quit_game()
                    elif snake.is_direction_enable(event.key):
                        new_dir = event.key
            if new_dir is not None:
                     snake.update_direction(new_dir )        
            
            
            self.screen.blit(self.bg_image,(0,0))   
            #网格线
            #pygame.draw.line( screen,COLOR_GRAY,(0,0),(300,100),1)
            #绘制所有的横线  h = 480 //20 = 24 [2,20,40,........480]
            for y in range (0,SCREEN_HEIGHT,BLOCK_SIZE):
                pygame.draw.line(self.screen,COLOR_GRAY,(0,y),(SCREEN_WIDTH,y))
            #绘制所有的竖线  w = 640 //20 = 32 [2,20,40,........640]
            for x in range (0,SCREEN_WIDTH,BLOCK_SIZE):
                pygame.draw.line(self.screen,COLOR_GRAY,(x,0),(x,SCREEN_WIDTH))

            #绘制蛇身
            snake.draw(self.screen)
            if not is_game_over:
                    
                snake.move()
                
                #遇到食物，吃掉，身体长一节
                snake_head = snake.snake_body[0]
                if snake.snake_body[0] == food.node:
                    #吃掉食物，刷新食物
                    food = Food(Food.gen_food_positison(snake))
                    #蛇身变长
                    snake.grow()
                    
                #碰到墙壁
                if snake_head.x < 0 or snake_head.x >= SCREEN_WIDTH \
                    or snake_head.y<0 or snake_head.y>=SCREEN_HEIGHT: 
                    is_game_over = True
                    
                
                #碰到自己
                if snake_head in snake.snake_body[1:]:
                    is_game_over = True
                    
                if is_game_over:
                    self.show_text("游戏结束",50,SCREEN_WIDTH//4,SCREEN_HEIGHT//4)
                    self.show_text("得分",24,SCREEN_WIDTH//4,SCREEN_HEIGHT//4+60)
                    self.show_text("按任意键重新开始",24,SCREEN_WIDTH//4,SCREEN_HEIGHT//4+90)
                    self.show_text("按Q退出游戏",24,SCREEN_WIDTH//4,SCREEN_HEIGHT//4+120)
                    
                #绘制食物
                food.draw(self.screen)

                #绘制得分和fps
                fps = clock.get_fps()
                self.show_text("FPS: {:.2f}".format(fps),20,SCREEN_WIDTH-200,10)
                
                self.show_text("Score: {}".format(snake.score),20,10,10)
                #刷新界面

                pygame.display.flip()  
                        
                clock.tick(5+snake.score)

    def quit_game(self):
        pygame.display.quit()
        exit(0)
            
    def show_text(self,text,font_size,x,y):
        font1 = pygame.font.SysFont('Simhei',font_size)
        text = font1.render(text,True,COLOR_RED)  
        self.screen.blit(text,(x,y))
           
           
if __name__ == '__main__':
    snake_game = Game()
    snake_game.start()
               

           